window.addEvent('domready', function() {
    function setPermsSignals() {
        var read_url = $('baseurl').getProperty('value') + '/index.php?option=com_docario&controller=category&task=changereadperm&format=component';
        var write_url = $('baseurl').getProperty('value') + '/index.php?option=com_docario&controller=category&task=changewriteperm&format=component';
    
        $$('.urs').addEvent('change', function() {
            new Ajax(read_url, {
                method: 'post',
                data: {
                    value: this.getProperty('value'),
                    uid: this.getProperty('id').substr(3),
                    cid: $('category_id').getProperty('value')
                },
                onSuccess: function (data) {
                    doSearch();
                }
            }).request();
        });
        
        $$('.uws').addEvent('change', function() {
            new Ajax(write_url, {
                method: 'post',
                data: {
                    value: this.getProperty('value'),
                    uid: this.getProperty('id').substr(3),
                    cid: $('category_id').getProperty('value')
                },
                onSuccess: function (data) {
                    doSearch();
                }
            }).request();
        });
    }
    
    function doSearch() {
        var search_value = $('user_search').getProperty('value');
        var url = $('baseurl').getProperty('value') + '/index.php?option=com_docario&controller=category&task=searchusers&format=component';
        var category_id = $('category_id').getProperty('value');
        
        if (search_value.length <= 3) {
            search_value = '';
        }
        
        
        new Ajax(url, {
            method: 'post',
            data: {
                'search': search_value,
                'category_id': category_id
            },
            onSuccess: function(data) {
                $('user_table').innerHTML = data;
                setPermsSignals();
            }
        }).request();
    }
    
    if ($('user_search')) {
        setPermsSignals();
        $('user_search').addEvent('keyup', function() {
            doSearch();
        });
    }
    
    //local view events
    $$('.grs').addEvent('change', function() {
        var url = $('baseurl').getProperty('value') + '/index.php?option=com_docario&controller=category&task=greadchange&tmpl=component';
        var category_id = $('category_id').getProperty('value');
        var value = this.getProperty('value');
        var gid = this.getProperty('id').substr(3);
        
        new Ajax(url, {
            method: 'post',
            data: {
                'cid': category_id,
                'gid': gid,
                'value': value
            },
            onSuccess: function(data) {
                if (value == 0) $('cgr'+gid).setProperty('class', 'permoff');
                else $('cgr'+gid).setProperty('class', 'permon');
            }
        }).request();
    });
    
    $$('.gws').addEvent('change', function() {
        var url = $('baseurl').getProperty('value') + '/index.php?option=com_docario&controller=category&task=gwritechange&tmpl=component';
        var category_id = $('category_id').getProperty('value');
        var value = this.getProperty('value');
        var gid = this.getProperty('id').substr(3);
        
        new Ajax(url, {
            method: 'post',
            data: {
                'cid': category_id,
                'gid': gid,
                'value': value
            },
            onSuccess: function(data) {
                if (value == 0) $('cgw'+gid).setProperty('class', 'permoff');
                else $('cgw'+gid).setProperty('class', 'permon');
            }
        }).request();
    });
    
    //start user events loadings and event handlers
    doSearch();
});
